﻿Imports System.IO
Imports System.Net
Imports Microsoft.WindowsAPICodePack.ApplicationServices

Public Class DesktopF

    Private MausPosition As Point
    Dim WithEvents client As WebClient = New WebClient()
    Dim Antivirus_App_1_Prozess As String
    Dim Antivirus_App_Label As String

#Region "Functions"

    Private Sub LoadFilesAndFolders_OfDesktop()
        For Each DesktopShortcut In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.Desktop)

            Dim ShortcutIcon As Icon = Icon.ExtractAssociatedIcon(DesktopShortcut)  ' Get the associated icon from the shortcut
            Dim FI As New FileInfo(DesktopShortcut)                                 ' Get the file informations

            DesktopImages.Images.Add(ShortcutIcon.ToBitmap)                         ' Add the associated icons from the shortcut to the imagelist

            With Desktop.Items.Add(Path.GetFileNameWithoutExtension(FI.FullName), DesktopImages.Images.Count - 1)
                .Tag = FI.FullName                                                  ' Add a tag to use it later
            End With

        Next

        For Each DesktopFolderShortcut In My.Computer.FileSystem.GetDirectories(My.Computer.FileSystem.SpecialDirectories.Desktop)

            Dim DI As New DirectoryInfo(DesktopFolderShortcut)                       ' Get the direcotry informations

            With Desktop.Items.Add(DI.Name, 0)                                       ' Use ImageIndex 0 for folder icon
                .Tag = DI.FullName                                                   ' Add a tag to use it later
            End With

        Next

    End Sub

    Private Sub Get_Battery_Status(ByVal StatusOrAndValues As Integer)

        If StatusOrAndValues = 0 Then

            If PowerManager.PowerSource.ToString = "AC" Then

                Energy_App.Image = My.Resources.Status_battery_charging_icon
                DesktopToolTips.SetToolTip(Energy_App, "Kabel")

            ElseIf PowerManager.PowerSource.ToString = "Battery" Then

                Energy_App.Image = My.Resources.Status_battery_100_icon
                DesktopToolTips.SetToolTip(Energy_App, "Batterie")

            ElseIf PowerManager.PowerSource.ToString = "Ups" Then

                Energy_App.Image = My.Resources.Status_battery_charging_icon
                DesktopToolTips.SetToolTip(Energy_App, "UPS")

            End If

        ElseIf StatusOrAndValues = 1 Then

            If PowerManager.PowerSource.ToString = "AC" Then
                DesktopToolTips.SetToolTip(Energy_App, "Kabel")
            ElseIf PowerManager.PowerSource.ToString = "Ups" Then
                DesktopToolTips.SetToolTip(Energy_App, "UPS")
            End If

            If PowerManager.IsBatteryPresent And PowerManager.PowerSource.ToString = "AC" Then
                DesktopToolTips.SetToolTip(Energy_App, "Aufladen " + PowerManager.BatteryLifePercent.ToString + "%")

                If PowerManager.BatteryLifePercent <= 100 Then
                    Energy_App.Image = My.Resources.Status_battery_charging_icon
                ElseIf PowerManager.BatteryLifePercent <= 75 Then
                    Energy_App.Image = My.Resources.Status_battery_charging_080_icon
                ElseIf PowerManager.BatteryLifePercent <= 50 Then
                    Energy_App.Image = My.Resources.Status_battery_charging_060_icon
                ElseIf PowerManager.BatteryLifePercent <= 25 Then
                    Energy_App.Image = My.Resources.Status_battery_charging_040_icon
                ElseIf PowerManager.BatteryLifePercent <= 5 Then
                    Energy_App.Image = My.Resources.Status_battery_charging_low_icon
                End If

            ElseIf PowerManager.IsBatteryPresent And PowerManager.PowerSource.ToString = "Battery" Then
                DesktopToolTips.SetToolTip(Energy_App, "Batterie " + PowerManager.BatteryLifePercent.ToString + "%")

                If PowerManager.BatteryLifePercent <= 100 Then
                    Energy_App.Image = My.Resources.Status_battery_100_icon
                ElseIf PowerManager.BatteryLifePercent <= 75 Then
                    Energy_App.Image = My.Resources.Status_battery_080_icon
                ElseIf PowerManager.BatteryLifePercent <= 50 Then
                    Energy_App.Image = My.Resources.Status_battery_060_icon
                ElseIf PowerManager.BatteryLifePercent <= 25 Then
                    Energy_App.Image = My.Resources.Status_battery_040_icon
                ElseIf PowerManager.BatteryLifePercent <= 5 Then
                    Energy_App.Image = My.Resources.Status_battery_caution_icon
                End If

            End If

        End If

    End Sub

    Private Sub SetStyles()
        Bot_Box.Parent = Desktop

        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(ControlStyles.UserPaint, True)
    End Sub

    Private Sub CheckGraphicsCard()
        If Directory.Exists(My.Computer.FileSystem.Drives(0).Name + "ATI") Then
            CatalystControlCenterToolStripMenuItem.Enabled = True
            NVIDIAControlCenterToolStripMenuItem.Enabled = False
        ElseIf Directory.Exists(My.Computer.FileSystem.Drives(0).Name + "NVIDIA") Then
            CatalystControlCenterToolStripMenuItem.Enabled = False
            NVIDIAControlCenterToolStripMenuItem.Enabled = True
        End If
    End Sub

    Private Function DoesProcessExists(ByVal PName As String) As Boolean
        Return System.Diagnostics.Process.GetProcessesByName(PName).Length >= 1
    End Function

    Private Sub Antivirus()

        If DoesProcessExists("ccsvchst") Then
            Antivirus_App.Image = My.Resources.Norton_Symantec_icon
            Antivirus_App_1_Prozess = "ccsvchst"
            Antivirus_App_Label = "Norton Internet Security"

            DesktopToolTips.SetToolTip(Antivirus_App, "Norton Internet Security")
        ElseIf DoesProcessExists("AvastSvc") Then
            Antivirus_App.Image = My.Resources.Avast_icon
            Antivirus_App_1_Prozess = "AvastSvc"
            Antivirus_App_Label = "Avast Antivirus"

            DesktopToolTips.SetToolTip(Antivirus_App, "Avast Antivirus")
        ElseIf DoesProcessExists("kavsvc") Then
            Antivirus_App.Image = My.Resources.Kaspersky_icon
            Antivirus_App_1_Prozess = "kavsvc"
            Antivirus_App_Label = "Kaspersky"

            DesktopToolTips.SetToolTip(Antivirus_App, "Kaspersky")
        Else
            Antivirus_App.Visible = False
        End If

    End Sub

    Public Function ReadLine(ByVal line As Integer) As String
        Try
            Dim lines As String() = System.IO.File.ReadAllLines(".\updates\updates.txt")

            If line > 0 Then
                Return lines(line - 1)
            ElseIf line < 0 Then
                Return lines(lines.Length + line - 1)
            Else
                Return ""
            End If

        Catch ex As Exception
            Return ""
        End Try
    End Function

    Private Sub CheckForUpdates()
        Try
            client.DownloadFileAsync(New Uri("http://85.31.189.150/other/luxdc/updates.txt"), ".\updates\updates.txt")
        Catch ex As Exception
        End Try
    End Sub

    Private Sub GetNetworkInfos(ByVal StatusOrValues As Integer)
        If StatusOrValues = 0 Then
            If My.Computer.Network.IsAvailable = True Then
                DesktopToolTips.SetToolTip(Network_App, "Internetzugriff")
            ElseIf My.Computer.Network.IsAvailable = False Then
                DesktopToolTips.SetToolTip(Network_App, "Keine Internetverbindung")
            End If
        End If
    End Sub

    Public Function GetShellLinkPath(ByVal datei As String) As String

        Dim oShell As New Shell32.Shell
        Dim oFolder As Shell32.Folder
        Dim oLink As Shell32.ShellLinkObject
        Dim sPath As String
        Dim sFile As String
        Dim sLinkFile As String = datei

        sPath = Path.GetDirectoryName(sLinkFile)
        sFile = Path.GetFileName(sLinkFile)
        oFolder = oShell.NameSpace(sPath)
        oLink = oFolder.Items.Item(sFile).GetLink

        Return oLink.WorkingDirectory

    End Function

#End Region

#Region "Timers"

    Private Sub System_Timer_Tick(sender As System.Object, e As System.EventArgs) Handles System_Timer.Tick
        Clocktime.Text = Format(Now, "HH:mm:ss") & vbNewLine & Format(Now, "dd.MM.yyyy")
    End Sub

    Private Sub Other_System_Services_Tick(sender As System.Object, e As System.EventArgs) Handles Other_System_Services.Tick
        Get_Battery_Status(0)
        GetNetworkInfos(0)
    End Sub

#End Region

#Region "ItemRightClickMenu Events"

    Private Sub OpenToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Try
            Process.Start(Desktop.SelectedItems(0).Tag)                                  ' Try to open the selected shortcut
        Catch ex As Exception
            MsgBox("Could not start selected shortcut", MsgBoxStyle.Critical, "Error")
        End Try
    End Sub

    Private Sub UmbennenToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles UmbennenToolStripMenuItem.Click
        Desktop.SelectedItems(0).BeginEdit()                                        ' Starts LabelEdit with selected shortcut
    End Sub

    Private Sub LöschenToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles LöschenToolStripMenuItem.Click
        If MsgBox("Möchten Sie wirklich die Datei " + Desktop.SelectedItems(0).Text + " löschen?", MsgBoxStyle.YesNo, "Datei löschen") = MsgBoxResult.Yes Then
            File.Delete(Desktop.SelectedItems(0).Tag)                               ' Deletes the selected shortcut if answered with YES
        End If
    End Sub

    Private Sub AlsAdministratorÖffnenToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AlsAdministratorÖffnenToolStripMenuItem.Click
        Dim PSInfo As New ProcessStartInfo

        PSInfo.FileName = Desktop.SelectedItems(0).Tag
        PSInfo.Verb = "runas"
        Process.Start(PSInfo)
    End Sub

    Private Sub DateipfadÖffnenToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles DateipfadÖffnenToolStripMenuItem.Click
        Process.Start(GetShellLinkPath(Desktop.SelectedItems(0).Tag))
    End Sub

    Private Sub EigenschaftenToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EigenschaftenToolStripMenuItem.Click
        Properties.Show()
    End Sub

#End Region

#Region "Desktop (F) Events"

    Private Sub DesktopF_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        LoadFilesAndFolders_OfDesktop()                                             ' Load Desktop Files, Shortcuts and Folders
        SetStyles()                                                                 ' Set Styles "For better performance"
        Antivirus()                                                                 ' Load other system informations ...
        Get_Battery_Status(0)                                                       ' Get Battery Status
        GetNetworkInfos(0)                                                          ' Get Network Status

        CheckForUpdates()                                                           ' ...
        CheckGraphicsCard()                                                         ' ...

        System_Timer.Start()                                                        ' Start System Timer for Clock
        Other_System_Services.Start()                                               ' Battery, Network status etc.

    End Sub

    Private Sub Desktop_AfterLabelEdit(sender As Object, e As System.Windows.Forms.LabelEditEventArgs) Handles Desktop.AfterLabelEdit
        If e.Label = Nothing Then
            Return
        End If

        Dim FI As New FileInfo(Desktop.Items(e.Item).Tag)                                        ' Get FileInfo of the edited Shortcut to open more possibilities
        File.Move(Desktop.Items(e.Item).Tag, FI.DirectoryName + "\" + e.Label + FI.Extension)    ' Rename the shortcut using File.Move

    End Sub

    Private Sub Desktop_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Desktop.MouseClick

        If e.Button = MouseButtons.Right Then
            If Desktop.SelectedItems.Count > 0 And Desktop.SelectedItems.Count < 2 Then
                ItemRightclickMenu.Show(MousePosition)                                   ' If a shortcut is selected, open the ItemRightClickMenu
            End If
        End If

        If e.Button = MouseButtons.Left Then
            If Desktop.SelectedItems.Count > 0 And Desktop.SelectedItems.Count < 2 Then
                Desktop.SelectedItems(0).BeginEdit()                                     ' If a shortcut gets selected with MouseButtons.Left then start editing (Windows Version -> 2 clicks with delay)
            End If
        End If

    End Sub

#End Region

    Private Sub Rightclickmenu_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Rightclickmenu.Opening
        If Desktop.SelectedItems.Count > 0 And Desktop.SelectedItems.Count < 2 Then
            e.Cancel = True                                                         ' Doesn't open the normal RightClickMenu on Shortcuts
        End If
    End Sub

    Private Sub client_DownloadFileCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles client.DownloadFileCompleted
        If ReadLine(1).Contains("verfuegbar") Then
            Update_App.Visible = True
            DesktopToolTips.SetToolTip(Update_App, "Update verfügbar")
        End If
    End Sub

End Class